import java.awt.Graphics;
import java.awt.image.ImageObserver;

public class Bonus extends GameObject{
	
	private int hspeed;
	private int value;
	private int showTime;
	private int animWait;
	private final int MAXSHOWTIME = 50*InvaderSaver.GAMESPEED_MODIFIER;
	private final int ANIM_COOLDOWN = 4*InvaderSaver.GAMESPEED_MODIFIER;
	private boolean alive;
	
	public Bonus(int x, int y, Sprite sprite, int hspeed){
		super(x, y, sprite);
		this.hspeed = hspeed;
		alive = true;
		// assign the bonus a value between 50 and 300, on increments of 50
		value = (int)(Math.random() * 6 + 1) * 50;
		showTime = 0;
		animWait = 0;
	}

	public void update(){
		if(alive){
			x_position += hspeed;
			animWait++;
			if(animWait>=ANIM_COOLDOWN){
				image_index++;
				animWait = 0;
			}
		}
		else{
			showTime++;
		}
	}
	
	public void die(){
		alive = false;
		HUD.modifyScore(value);
	}
	
	public Boolean isAlive(){
		return alive;
	}
	
	public Boolean resetMe(){
		if((!alive && showTime >= MAXSHOWTIME) || x_position < 0-sprite.image_width/2 && hspeed<0 || x_position > InvaderSaver.getGameWidth()+sprite.image_width/2 && hspeed>0){
			return true;
		}
		return false;
	}
	
	public int getSpeed(){
		return hspeed;
	}
	
	public void draw(Graphics g, ImageObserver io){
		if(alive){
			g.drawImage(sprite.getFrame(image_index), x_position-sprite.image_width/2, y_position-sprite.image_height/2, io);
		}
		else{
			int digits = 2; // will either be 2 or three
			// set i equal to the number of digits in our number. Use this info to center the number
			while(Math.floor((double)value / Math.pow(10, digits)) > 0.0){
				digits++;
			}
			HUD.drawNumber(g, io, x_position-sprite.image_width/2+(sprite.image_width/2 - (SpriteLib.NUMBERS.image_width+6)*digits/2), 
					y_position-sprite.image_width/2+(sprite.image_width/2-SpriteLib.NUMBERS.image_height/2), value);
		}
	}
}
